package com.atguigu.spzx.product.controller;

import com.atguigu.spzx.model.entity.product.Category;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.h5.IndexVo;
import com.atguigu.spzx.product.service.CategoryService;
import com.atguigu.spzx.product.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * ClassName: IndexController
 * Package:
 * Description:
 *
 * @Author:xiaohei
 * @Create 2024/5/22 9:22
 * Version 1.0
 */
@Tag(name = "首页接口管理")
@RestController
@RequestMapping(value="/api/product/index")
//@CrossOrigin  //临时解决跨域问题
public class IndexController{

    @Resource
    private CategoryService categoryService;
    @Resource
    private ProductService productService;

    @Operation(summary = "获取首页数据")
    @GetMapping
    public Result index(){
        //1 所有一级分类
        List<Category> categoryList = categoryService.selectOneCategory();

        //2  根据销量排序，获取前十条记录
        List<ProductSku> productSkuList = productService.selectProductSkuBySal();

        //3 返回  使用IndexVo
        IndexVo indexVo = new IndexVo();
        indexVo.setCategoryList(categoryList);
        indexVo.setProductSkuList(productSkuList);

        return Result.build(indexVo, ResultCodeEnum.SUCCESS);
    }


}
